//
//  BLVideoView.m
//  百思不得姐
//
//  Created by hbl on 16/3/26.
//  Copyright © 2016年 hbl. All rights reserved.
//

#import "BLVideoView.h"
#import <YYAnimatedImageView.h>
#import "M13ProgressViewRing.h"
#import "BLBigImageViewController.h"
#import <YYWebImage.h>
#import "BLPlayVIewViewController.h"
#import "Video.h"
#import "BLPlayVIewViewController.h"
@interface BLVideoView()<MyVideoDelegate>
@property (weak, nonatomic) IBOutlet YYAnimatedImageView *pictureView;

@property (weak, nonatomic) IBOutlet M13ProgressViewRing *progressView;
@property (weak, nonatomic) IBOutlet UILabel *playCount;
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;
@property (nonatomic, strong)Video *videoPlayer;
@end


@implementation BLVideoView

+ (instancetype)videoView
{
    return [[[NSBundle mainBundle]loadNibNamed:@"BLVideoView" owner:nil options:nil] lastObject];;
}

- (void)awakeFromNib
{
    self.autoresizingMask = UIViewAutoresizingNone;
    self.progressView.secondaryColor = [UIColor whiteColor];
    self.progressView.primaryColor = [UIColor whiteColor];
    self.progressView.hidden = YES;
    [self.pictureView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onClickPicture:)]];
    
}

- (void)onClickPicture:(UITapGestureRecognizer *)tap
{
    BLBigImageViewController *bigImage = [[BLBigImageViewController alloc]init];
    bigImage.topic = self.topic;
    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:bigImage animated:YES completion:nil];
}

- (IBAction)play:(id)sender {
//    BLPlayVIewViewController *playVC = [[BLPlayVIewViewController alloc]init];
//    playVC.urlStr = self.topic.videouri;
//    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:playVC animated:YES completion:nil];
    [self initMyVideoPlayer];
}

- (void)setTopic:(BLTopic *)topic
{
    _topic = topic;
    [self.progressView setProgress:0.0 animated:NO];
    
    [self.pictureView yy_setImageWithURL:[NSURL URLWithString:topic.image2] placeholder:nil options:YYWebImageOptionSetImageWithFadeAnimation progress:^(NSInteger receivedSize, NSInteger expectedSize) {
        self.progressView.hidden = NO;
        CGFloat progress = ((CGFloat)receivedSize)/((CGFloat)expectedSize);
        [self.progressView setProgress:1.0 * progress animated:YES];
    } transform:nil completion:^(UIImage * _Nullable image, NSURL * _Nonnull url, YYWebImageFromType from, YYWebImageStage stage, NSError * _Nullable error) {
        [self.progressView setProgress:1.0 animated:YES];
        self.progressView.hidden = YES;
    }];
    
    long min = topic.videotime/60;
    long second = topic.videotime%60;
    self.timeLabel.text = [NSString stringWithFormat:@"%02ld:%02ld",min,second];
    
    self.playCount.text = [NSString stringWithFormat:@"播放%ld次",topic.playcount];
    
    if (topic.picLong) {
        
        self.pictureView.contentMode = UIViewContentModeScaleAspectFill;
        self.pictureView.clipsToBounds = YES;
    }else{
        
        self.pictureView.contentMode = UIViewContentModeScaleToFill;
    }
   
}

- (void)initMyVideoPlayer
{
    self.videoPlayer = [Video playVideo];
    self.videoPlayer.delegate = self;
    self.videoPlayer.frame = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height );
    [self addSubview:self.videoPlayer];
    //  NSURL *url = [[NSBundle mainBundle]URLForResource:self.urlStr withExtension:nil];
    NSURL *url = [NSURL URLWithString:self.topic.videouri];
    AVPlayerItem *item = [[AVPlayerItem alloc]initWithURL:url];
    [self.videoPlayer setItem:item];
}

- (void)toFull:(BOOL)isFull
{
    if (isFull) {
        [[UIApplication sharedApplication].keyWindow.rootViewController dismissViewControllerAnimated:YES completion:^{
            [UIView animateWithDuration:0.5 animations:^{
                [self.videoPlayer.myPlayer pause];
                [self.videoPlayer removeFromSuperview];
                [self.videoPlayer.myPlayer replaceCurrentItemWithPlayerItem:nil];
                
            }];
            
        }];
    }else{
        BLPlayVIewViewController *fvc = [[BLPlayVIewViewController alloc]init];
        [[UIApplication sharedApplication].keyWindow.rootViewController  presentViewController:fvc animated:YES completion:^{
            [UIView animateWithDuration:0.5 animations:^{
                self.videoPlayer.frame = CGRectMake(0, 0, fvc.view.frame.size.width, fvc.view.frame.size.height);
                [fvc.view addSubview:self.videoPlayer];
            }];
        }];
    }

}

@end
